package com.qyhl.webtv.module_broke.utils.radiobutton;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.module_broke.R;

import java.io.IOException;

/**
 * Created by qyhl on 2017/8/29.
 */

public class ScoopRadioButton extends RelativeLayout implements MediaPlayer.OnPreparedListener, View.OnClickListener, MediaPlayer.OnCompletionListener {
    private ImageView mAnimaView, mPlayerBtn;
    private TextView mPlayerDuration, mDelete;

    private OnDeleteListener deleteListener;
    private String voicePath;
    private View view;

    private RelativeLayout playerLayout;

    AnimationDrawable animationDrawable;
    MediaPlayer mPlayer;

    private long dur;


    public void setDeleteListener(OnDeleteListener deleteListener) {
        this.deleteListener = deleteListener;
    }

    public ScoopRadioButton(Context context) {
        super(context);
        initView(context);
    }

    public ScoopRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ScoopRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        view = LayoutInflater.from(context).inflate(R.layout.broke_layout_scoop_radio, this, true);
        mAnimaView = (ImageView) view.findViewById(R.id.scoop_radio_anima);
        mPlayerBtn = (ImageView) view.findViewById(R.id.radio_player_state);
        mPlayerDuration = (TextView) view.findViewById(R.id.radio_duration);
        mDelete = (TextView) view.findViewById(R.id.delete);
        playerLayout = (RelativeLayout) view.findViewById(R.id.player_layout);
        mDelete.setOnClickListener(this);
        playerLayout.setOnClickListener(this);
    }

    /**
     * 设置时长
     *
     * @param player
     */
    public void setDuration(MediaPlayer player) {
        dur = player.getDuration();
        mPlayerDuration.setText(formatTime(dur));
    }

    public long getDuration() {
        return dur;
    }

    /*
    * 毫秒转化时分秒毫秒
    */
    public String formatTime(Long ms) {
        if (ms > (59 * 1000)) {
            int miao = (int) (ms / 1000);
            int minutes = miao / 60;
            int second = miao - 60 * minutes;
            return minutes + "'" + second + "\"";
        } else {
            return ms / 1000 + "\"";
        }
    }

    //启动动画
    public void startAnimation() {
        mAnimaView.setImageResource(R.drawable.scoop_voice_start);
        animationDrawable = (AnimationDrawable) mAnimaView.getDrawable();
        animationDrawable.start();
    }

    //关闭动画
    public void endAnimation() {
        mAnimaView.setImageResource(R.drawable.scoop_voice_end);
        animationDrawable = (AnimationDrawable) mAnimaView.getDrawable();
        animationDrawable.start();
    }

    public interface OnDeleteListener {
        void deleteEvent();
    }

    public void setPlayerPath(String path) {
        mPlayer = new MediaPlayer();
        voicePath = path;
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mPlayer.setOnPreparedListener(this);
        mPlayer.setOnCompletionListener(this);
        mPlayer.reset();
        try {
            mPlayer.setDataSource(voicePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mPlayer.prepare();//prepare之后自动播放
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        endAnimation();
        mPlayerBtn.setImageResource(R.drawable.radio_player_btn);
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        setDuration(mp);
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.delete) {
            deleteListener.deleteEvent();//删除事件

        } else if (i == R.id.player_layout) {
            if (StringUtils.isEmpty(voicePath)) {
                return;
            }
            if (mPlayer.isPlaying()) {
                mPlayer.pause();
                endAnimation();
                mPlayerBtn.setImageResource(R.drawable.radio_player_btn);
            } else {
                mPlayer.start();
                startAnimation();
                mPlayerBtn.setImageResource(R.drawable.radio_player_stop);
            }

        } else {
        }
    }

    /**
     * 释放资源
     */
    public void release() {
        if (mPlayer != null && mPlayer.isPlaying()) {
            mPlayer.stop();
            mPlayer.release();
            mPlayerBtn.setImageResource(R.drawable.radio_player_btn);
            endAnimation();
        }
    }

}
